// $codepro.audit.disable booleanMethodNamingConvention
/**
 * Project: Utilities <br>
 * Package: org.leumasjaffe.functional
 */
package org.leumasjaffe.functional;

import java.util.Comparator;

import org.eclipse.jdt.annotation.NonNullByDefault;

/**
 * Class org.leumasjaffe.functional.ComparatorFunction in file ComparatorFunction.java <br>
 * Provides a bridge between Comparator and BinaryFunction
 * 
 * @author leumasjaffe
 * @version Nov 25, 2013
 * Created on: Nov 25, 2013
 * 
 * @param <T>
 */
@NonNullByDefault
public class CompareOperator<T> implements BinaryFunction<T, T, Boolean> {
	public static enum Compare {
		GREATER_THAN {
			@Override
			public boolean CMP(int value) {
				return value > 0;
			}
		}, GREATER_THAN_OR_EQUAL {
			@Override
			public boolean CMP(int value) {
				return value >= 0;
			}
		}, LESS_THAN {
			@Override
			public boolean CMP(int value) {
				return value < 0;
			}
		}, LESS_THAN_OR_EQUAL {
			@Override
			public boolean CMP(int value) {
				return value <= 0;
			}
		}, EQUAL {
			@Override
			public boolean CMP(int value) {
				return value == 0;
			}
		}, NOT_EQUAL {
			@Override
			public boolean CMP(int value) {
				return value != 0;
			}
		};

		/**
		 * Compares the value given against zero in a pre-defined way
		 * @param value An integer
		 * @return The whether the integer shows the correct comparison value
		 */
		public abstract boolean CMP(int value);
	}
	
	private final Comparator<? super T> compare;
	private final Compare cmp;
	
	/**
	 * @param compare A comparator to generate ordering
	 * @param cmp The ordering to use
	 */
	public CompareOperator(Comparator<? super T> compare, Compare cmp) {
		this.compare = compare;
		this.cmp = cmp;
	}
	
	/* (non-Javadoc)
	 * @see org.leumasjaffe.functional.BinaryFunction#operate(java.lang.Object, java.lang.Object)
	 */
	@SuppressWarnings("null")
	@Override
	public final Boolean apply(T t1, T t2) {
		return Boolean.valueOf(this.cmp.CMP(this.compare.compare(t1, t2)));
	}

}
